package net.zjitc.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import net.zjitc.common.ResponseResult;
import net.zjitc.entity.Manger;
import net.zjitc.entity.Role;
import net.zjitc.entity.Vo.MangerRoleVo;
import net.zjitc.service.MangerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin
@RestController
public class MangerController {
    @Autowired
    private MangerService mangerService;
    ResponseResult result=new ResponseResult();


    @GetMapping("/user")
    public ResponseResult selectAll(){
        List<Manger> mangers = mangerService.selectAll();


        result.setCreateSuccess("查询成功",mangers);
        return result;
    }

    /**
     *分页查询
     * @param query
     * @param pagenum
     * @param pagesize
     * @return
     */
    @GetMapping("/users")
    public ResponseResult getUsers(@RequestParam(required = false) Integer query,
                                   @RequestParam(required = true ,value = "pagenum") Integer pagenum,
                                   @RequestParam(required = true,value = "pagesize") Integer pagesize){
        Page<MangerRoleVo> page=new Page<>(pagenum,pagesize);
        Page<MangerRoleVo> vos = mangerService.selectPageVo(page);
        ResponseResult result=new ResponseResult();

        result.setCreateSuccess("查询成功",vos);
        return result;
    }


    /**
     * 添加
     * @param mangerRoleVo
     * @return
     */
    @PostMapping("/users")
    public ResponseResult add(@RequestBody MangerRoleVo mangerRoleVo){
        //先判断此用户是否存在，如果不存在，则添加
       Manger manger=mangerService.selectByUsername(mangerRoleVo.getUsername());
       if(manger!=null){
           result.setError("此用户已存在");
       }else {
           Integer i=mangerService.addUser(mangerRoleVo);
           result.setCreateSuccess("用户创建成功",mangerRoleVo);

       }
        return result;
    }

    /**
     * 根据id修改用户状态
     * @param id
     * @param type
     * @return
     */
    @PutMapping("/users/{uid}/state/{type}")
    public ResponseResult updateType(@PathVariable(value = "uid") Integer id,@PathVariable boolean type){
        //查询id是否存在
        Manger manger=mangerService.selectById(id);
        if(manger==null){
            result.setError("此用户不存在");
        }else {
            Integer integer = mangerService.updateType(id, type);
            if (integer!=1){
                result.setError("设置状态失败");
            }else {
                Integer myInt = type ? 1 : 0;
                manger.setType(myInt);
                result.setSuccess("设置状态成功",manger);
            }
        }

        return result;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/users/{id}")
    public ResponseResult selectById(@PathVariable Integer id){
        Manger manger=mangerService.selectById(id);
        if(manger==null){
          result.setSuccess("此用户不存在");
        }else {
            result.setSuccess("查询成功",manger);
        }
        return result;
    }

    /**
     * 根据id修改用户
     * @param id
     * @param manger
     * @return
     */
    @PutMapping("/users/{id}")
    public ResponseResult UpdateUser(@PathVariable Integer id,@RequestBody Manger manger){

        Integer integer = mangerService.updateUser(id, manger);
        if (integer!=1){
            result.setError("更新失败");
        }else {
            Manger manger1= mangerService.selectById(id);
            result.setSuccess("更新成功",manger1);
        }
        return result;

    }

    /**
     * 根据id删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/users/{id}")
    public ResponseResult deleteUser(@PathVariable Integer id){
        //查询id是否存在
        Manger manger=mangerService.selectById(id);
        if(manger==null){
            result.setError("此用户不存在");
        }else {
            Integer integer = mangerService.deleteUser(id);
            if (integer != 1) {
                result.setError("删除失败");
            } else {
                result.setSuccess("删除成功", null);
            }
        }
        return result;
    }

    /**
     * 根据id修改分配用户权限
     * @param id
     * @param rid
     * @return
     */
    @PutMapping("/users/{id}/role")
    public ResponseResult assignPer(@PathVariable Integer id, @RequestParam String rid){
        Integer integer=mangerService.assignPer(id,Integer.parseInt(rid));
        if (integer!=1){
            result.setError("设置角色失败");
        }else {
            Manger manger1= mangerService.selectById(id);
            result.setSuccess("设置角色成功",manger1);
        }
        return result;
    }


}













